<?php

class Iris_Class_Pager
{
	private $current = 0;
	private $perPage = 0;
	private $total = 0;

	private $firstPage = 0;
	private $currentPage = 0;
	private $totalPage = 0;
	private $indexOffset = 3;

	private $firstLabel = 'First';
	private $previousLabel = '<';
	private $nextLabel = '>';
	private $lastLabel = 'Last';
	private $url = null;
	private $separator = ' | ';
	private $startName = 'start';

	private $LANG = null;

	// For example :
	// $page_navi = new suin_pager(array('current' => 0, 'perPage' => 5, 'total' => $total, 'url' => $_SERVER['SCRIPT_NAME']));
	function __construct($param)
	{
// 		$this->LANG = suin_get_language('Suin', 'Pager');
// 		$this->firstLabel = $this->LANG->FIRST_PAGE;
// 		$this->previousLabel = $this->LANG->PREVIOUS_PAGE;
// 		$this->nextLabel = $this->LANG->NEXT_PAGE;
// 		$this->lastLabel = $this->LANG->LAST_PAGE;
		$this->current = intval($param['current']);
		$this->perPage = intval($param['perPage']);
		$this->total = intval($param['total']);
		$this->currentPage = intval(floor($param['current'] / $param['perPage']));
		$this->totalPage = intval(ceil($param['total'] / $param['perPage']));
		$this->url = $param['url'];
		if(isset($param['indexOffset'])) $this->indexOffset = intval($param['indexOffset']);
		if(isset($param['firstLabel'])) $this->firstLabel = $param['firstLabel'];
		if(isset($param['previousLabel'])) $this->previousLabel = $param['previousLabel'];
		if(isset($param['nextLabel'])) $this->nextLabel = $param['nextLabel'];
		if(isset($param['lastLabel'])) $this->lastLabel = $param['lastLabel'];
		if(isset($param['separator'])) $this->separator = $param['separator'];
		if(isset($param['startName'])) $this->startName = $param['startName'];
	}

	function isAbleLeftLinker()
	{
		if( $this->totalPage >= 2 and $this->currentPage - $this->firstPage >= 1 ){
			return true;
		}else{
			return false;
		}
	}

	function isAbleRightLinker()
	{
		if( $this->totalPage >= 2 and $this->totalPage  - $this->currentPage >= 2 ){
			return true;
		}else{
			return false;
		}
	}

	function getFirstPage()
	{
		return $this->firstPage;
	}

	function getPreviousPage()
	{
		return ($this->currentPage <= $this->firstPage) ? $this->currentPage : $this->currentPage - 1;
	}

	function getTotalPage()
	{
		return $this->totalPage;
	}

	function getNextPage()
	{
		return ($this->currentPage >= $this->totalPage) ? $this->currentPage : $this->currentPage + 1;
	}

	function getLastPage()
	{
		return $this->totalPage - 1;
	}

	function getCurrentPage()
	{
		return $this->currentPage;
	}

	function getIndex()
	{
		$ret = array();
		$leftLack = $this->indexOffset - abs($this->firstPage - $this->currentPage);
		$rightLack = $this->indexOffset - abs($this->totalPage - $this->currentPage);
		if ( $leftLack > 0 ) {
			$offset = $this->indexOffset + $leftLack;
		} elseif ($rightLack > 0 ) {
			$offset = $this->indexOffset + $rightLack;
		} else {
			$offset = $this->indexOffset;
		}
		for ( $page = $this->firstPage ; $page < $this->totalPage ; $page++ ) {
			if ( abs($page - $this->currentPage) <= $offset ) {
				$ret[] = $page;
			}
		}
		return $ret;
	}

	function getUrlWithPage($page)
	{
		if( parse_url($this->url, PHP_URL_QUERY) ){ // PHP 5.1.2 or later
			return $this->url.'&'.$this->startName.'='.intval($page);
		}else{
			return $this->url.'?'.$this->startName.'='.intval($page);
		}
	}

	function getNavigationAsArray()
	{
		$ret = array();
		$ret[] = array(
			'label' => $this->firstLabel,
			'name' => 'first',
			'start' => $this->getFirstPage() * $this->perPage,
			'link' => $this->isAbleLeftLinker()
		);
		$ret[] = array(
			'label' => $this->previousLabel,
			'name' => 'previous',
			'start' => $this->getPreviousPage() * $this->perPage,
			'link' => $this->isAbleLeftLinker()
		);
		$indexPages = $this->getIndex();
		foreach( $indexPages as $indexPage ){
			$ret[] = array(
				'label' => $indexPage + 1,
				'name' => ($this->getCurrentPage() == $indexPage) ? 'current' : 'index',
				'start' => $indexPage * $this->perPage,
				'link' => ($this->getCurrentPage() == $indexPage) ? false : true
			);
		}
		$ret[] = array(
			'label' => $this->nextLabel,
			'name' => 'next',
			'start' => $this->getNextPage() * $this->perPage,
			'link' => $this->isAbleRightLinker()
		);
		$ret[] = array(
			'label' => $this->lastLabel,
			'name' => 'last',
			'start' => $this->getLastPage() * $this->perPage,
			'link' => $this->isAbleRightLinker()
		);
		return $ret;
	}

	function getNavigationAsHtml()
	{
		if ( $this->totalPage <= 1 ) return false;
		$html_arr = array();
		$indexPages = $this->getNavigationAsArray();
		foreach( $indexPages as $indexPage ){
			if( $indexPage['link'] === true ){
				$html_arr[] = '<a href="'.htmlspecialchars($this->getUrlWithPage($indexPage['start']), ENT_QUOTES).'" class="suin_pager '.$indexPage['name'].'">'.$indexPage['label'].'</a>';
			}else{
				$html_arr[] = '<span class="suin_pager '.$indexPage['name'].'">'.$indexPage['label'].'</span>';
			}
		}
		return implode($this->separator, $html_arr);
	}

	function assignNavigation(&$tpl, $name = 'page_navi')
	{
		$tpl->assign($name, $this->getNavigationAsArray());
	}

	function assignNavigationAsHtml(&$tpl, $name = 'page_navi')
	{
		$tpl->assign($name, $this->getNavigationAsHtml());
	}
}
?>
